# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# READ IN OUR DATA -------------------------------------------------------------
# Focus on the prize-bin-specific estimates across time aggregations

per_adult_total_empl_income_dt <- list()

per_adult_total_empl_income_dt[[1]] <-
    readRDS("~/estimation-output/wealth_effect_estimates_per_adult_total_empl_income_30K_300K.rds") #nolint
per_adult_total_empl_income_dt[[1]][, prize_size_label := "30K - 300K"]

per_adult_total_empl_income_dt[[2]] <-
    readRDS("~/estimation-output/wealth_effect_estimates_per_adult_total_empl_income_300K_1M.rds") #nolint
per_adult_total_empl_income_dt[[2]][, prize_size_label := "300K - 1M"]

per_adult_total_empl_income_dt[[3]] <-
    readRDS("~/estimation-output/wealth_effect_estimates_per_adult_total_empl_income_1Mplus.rds") #nolint
per_adult_total_empl_income_dt[[3]][, prize_size_label := "1M+"]

per_adult_total_empl_income_dt <-
    rbindlist(per_adult_total_empl_income_dt, use.names = TRUE)

setnames(per_adult_total_empl_income_dt, "ref_event_time", "event_time")

per_adult_total_empl_income_dt <-
    per_adult_total_empl_income_dt[event_time >= 97]

total_employment_dt <- list()

total_employment_dt[[1]] <-
    readRDS("~/estimation-output/wealth_effect_estimates_total_employment_30K_300K.rds") #nolint
total_employment_dt[[1]][, prize_size_label := "30K - 300K"]

total_employment_dt[[2]] <-
    readRDS("~/estimation-output/wealth_effect_estimates_total_employment_300K_1M.rds") #nolint
total_employment_dt[[2]][, prize_size_label := "300K - 1M"]

total_employment_dt[[3]] <-
    readRDS("~/estimation-output/wealth_effect_estimates_total_employment_1Mplus.rds") #nolint
total_employment_dt[[3]][, prize_size_label := "1M+"]

total_employment_dt <-
    rbindlist(total_employment_dt, use.names = TRUE)

setnames(total_employment_dt, "ref_event_time", "event_time")

total_employment_dt <-
    total_employment_dt[event_time >= 97]

# Total Employment actually estimated as non-employment
# so just adjust by multiplying by -1
total_employment_dt[, estimate := (-1) * estimate]

# Produce graph labels

per_adult_total_empl_income_dt[
    ,
    prize_size_label :=
    factor(
        prize_size_label,
        levels = c("30K - 300K", "300K - 1M", "1M+")
    )
]

per_adult_total_empl_income_dt[, time_window_label := as.character(NA)]
per_adult_total_empl_income_dt[
    event_time == 99, time_window_label := "Avg (+1 to +5)"
]
per_adult_total_empl_income_dt[
    event_time == 97, time_window_label := "Short Run (+1 to +2)"
]
per_adult_total_empl_income_dt[
    event_time == 98, time_window_label := "Long Run (+3 to +5)"
]
per_adult_total_empl_income_dt[
    ,
    time_window_label :=
        factor(
            time_window_label,
            levels = c("Avg (+1 to +5)", "Short Run (+1 to +2)", "Long Run (+3 to +5)") #nolint
        )
]

total_employment_dt[
    ,
    prize_size_label :=
    factor(
        prize_size_label,
        levels = c("30K - 300K", "300K - 1M", "1M+")
    )
]

total_employment_dt[, time_window_label := as.character(NA)]
total_employment_dt[
    event_time == 99, time_window_label := "Avg (+1 to +5)"
]
total_employment_dt[
    event_time == 97, time_window_label := "Short Run (+1 to +2)"
]
total_employment_dt[
    event_time == 98, time_window_label := "Long Run (+3 to +5)"
]
total_employment_dt[
    ,
    time_window_label :=
        factor(
            time_window_label,
            levels = c("Avg (+1 to +5)", "Short Run (+1 to +2)", "Long Run (+3 to +5)") #nolint
        )
]


# Figure B.6 -- wealth effects across time and by prize size

# Just to make it readable against to first, dark bar
errorbar_palette <- c("#999999",
                      "#000000",
                      "#000000")

figure_b_6_a <-
    ggplot(
        data = per_adult_total_empl_income_dt,
        aes(
            x = factor(time_window_label),
            y = (estimate * 100),
            fill = factor(prize_size_label)
        )
    ) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(
        ymin = ((estimate - (1.64 * cluster_se)) * 100),
        ymax = ((estimate + (1.64 * cluster_se)) * 100),
        color = factor(prize_size_label)
    ),
    position = position_dodge(0.9),
    show.legend = FALSE,
    width = 0.2
    ) +
    theme_bw(base_size = 11.5) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank()
    ) +
    scale_y_continuous(breaks = (seq.int(from = -7, to = 0, by = 1))) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines")
    ) +
    labs(x = "Timing", y = "Effect per $100 of Lottery Winnings") +
    geom_vline(xintercept = 1.5, linetype = 3) +
    scale_fill_viridis_d() +
    scale_color_manual(values = errorbar_palette) +
    coord_cartesian(ylim = c(-7, 0))

ggsave(
    plot = figure_b_6_a,
    filename = "~/paper/figures/figure-B.6a.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_6_a,
    filename = "~/paper/figures/figure-B.6a.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

figure_b_6_b <-
    ggplot(
        data = total_employment_dt,
        aes(
            x = factor(time_window_label),
            y = (estimate * 100000 * 100),
            fill = factor(prize_size_label)
        )
    ) +
    geom_bar(stat = "identity", position = position_dodge()) +
    geom_errorbar(aes(
        ymin = ((estimate - (1.64 * cluster_se)) * 100000 * 100),
        ymax = ((estimate + (1.64 * cluster_se)) * 100000 * 100),
        color = factor(prize_size_label)
    ),
    position = position_dodge(0.9),
    show.legend = FALSE,
    width = 0.2
    ) +
    theme_bw(base_size = 11.5) +
    theme(
        panel.grid.minor = element_blank(), # remove actual gridlines
        panel.grid.major = element_blank()
    ) +
    scale_y_continuous(breaks = (seq.int(from = -14, to = 0, by = 2))) +
    theme(
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(1.75, "lines")
    ) +
    labs(x = "Timing", y = "Effect per $100,000 of Lottery Winnings") +
    geom_vline(xintercept = 1.5, linetype = 3) +
    scale_fill_viridis_d() +
    scale_color_manual(values = errorbar_palette) +
    coord_cartesian(ylim = c(-14, 0))

ggsave(
    plot = figure_b_6_b,
    filename = "~/paper/figures/figure-B.6b.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_6_b,
    filename = "~/paper/figures/figure-B.6b.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
per_adult_total_empl_income_dt <- NULL
total_employment_dt <- NULL
errorbar_palette <- NULL
figure_b_6_a <- NULL
figure_b_6_b <- NULL
rm(
    per_adult_total_empl_income_dt,
    total_employment_dt,
    errorbar_palette,
    figure_b_6_a,
    figure_b_6_b
)